package ca.jerg.wizard;

import com.nexes.wizard.*;

public class ErgWizardStartDescriptor extends WizardPanelDescriptor
{
    public static final String IDENTIFIER = "START_PANEL";
    
    private ErgWizard ergWizard;
    private ErgWizardStartPanel start;
    private ErgWizardSetDescriptor firstSet;
    private static int currentSets = 0;
    
    public ErgWizardStartDescriptor( ErgWizardSetDescriptor set, ErgWizard erg ) 
    {
        super(IDENTIFIER,new ErgWizardStartPanel());
        
        // reference to the controls on the start panel
        start = (ErgWizardStartPanel)super.getPanelComponent();
        
        // reference to the first set panel
        firstSet = set;
        
        // reference to the top level wizard
        ergWizard = erg;
    }
    
    public Object getNextPanelDescriptor() 
    {
        return ErgWizardSetDescriptor.IDENTIFIER;
    }
    
    public Object getBackPanelDescriptor() 
    {
        return null;
    }
        
    public void aboutToHidePanel()
    {
    	int numSets = Integer.parseInt(start.getNumSets());
    	
    	// update the threshold power
    	ergWizard.setThresholdWatts(Double.parseDouble(start.getThresholdPower()));
    	
    	// update the initial set
    	if ( (numSets > 0) && (currentSets != numSets) )
    	{
    		int i;
    		Wizard wizard = super.getWizard();
        	ErgWizardSetDescriptor setPanel;

        	currentSets = numSets;

        	// only the default set
       		firstSet.setDefaultPanels();
        	if ( numSets == 1 )
        		return;    	
        	
	    	// build and link together the new sets
	    	for ( i=1; i<numSets; i++ )
	    	{
	    		String id = ErgWizardSetDescriptor.ID_PREFACE + i;
	            setPanel = new ErgWizardSetDescriptor(i,ergWizard);
	            wizard.registerWizardPanel(id,setPanel);
	            
	            if ( i < (numSets - 1) )
	            {
            		setPanel.setBackPanelDescriptor(ErgWizardSetDescriptor.ID_PREFACE + (i - 1));
	            	setPanel.setNextPanelDescriptor(ErgWizardSetDescriptor.ID_PREFACE + (i + 1));
	            }
	            else
	            {	            	
	            	// the last of the new set panels
            		setPanel.setBackPanelDescriptor(ErgWizardSetDescriptor.ID_PREFACE + (i - 1));	            	
	            	setPanel.setNextPanelDescriptor(firstSet.getNextPanelDescriptor());
	            
	            	// update the initial set panel
	            	firstSet.setNextPanelDescriptor(ErgWizardSetDescriptor.ID_PREFACE + 1);
	            }
	    	}
    	}
    }
}
